﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SCIE_DTL;
using System.Data;
using System.Data.SqlClient;


namespace SCIE_DAL
{
    /// <summary>
    /// Criada em:
    /// Por:
    /// Descrição:
    /// </summary>
    public class SoftwareDAO
    {
        #region Atributos

        protected DataFactory factory = new DataFactory();

        #endregion

        #region Métodos Básicos

        public void InserirSoftware()
        {}

        public void AlterarSoftware()
        {}

        public SoftwareDTO BuscarSoftware(int etiqueta)
        {
            IDbCommand comando = CriarComandoBuscarSoftware(etiqueta);
            try
            {
                comando.Connection.Open();
                IDataReader reader = comando.ExecuteReader();
                if (reader.Read())
                {
                    SoftwareDTO item = new SoftwareDTO();
                    PopularDto(reader, item);
                    return item;
                }
                else
                    return null;
            }
            catch (SqlException)
            {
                throw new Exception("Erro junto a comunicação com o banco");
            }
            catch (Exception)
            {
                throw new Exception("Erro inesperado");
            }
            finally
            {
                comando.Dispose();
            }
        }        

        public List<SoftwareDTO> BuscarTodosSoftwares()
        {
            IDbCommand comando = CriarComandoBuscarSoftware();
            try
            {
                comando.Connection.Open();
                IDataReader reader = comando.ExecuteReader();
                List<SoftwareDTO> lista = new List<SoftwareDTO>();
                while (reader.Read())
                {
                    SoftwareDTO item = new SoftwareDTO();
                    PopularDto(reader, item);
                    lista.Add(item);
                }                
                return lista;
            }
            catch (SqlException)
            {
                throw new Exception("Erro junto a comunicação com o banco");
            }
            catch (Exception)
            {
                throw new Exception("Erro inesperado");
            }
            finally
            {
                comando.Dispose();
            }
        }

        public SoftwareDTO BuscarSoftwarePorColuna(string nomeColuna, string valorColuna)
        {
            IDbCommand comando = CriarComandoBuscarSoftware(nomeColuna, valorColuna);
            try
            {
                comando.Connection.Open();
                IDataReader reader = comando.ExecuteReader();
                if (reader.Read())
                {
                    SoftwareDTO item = new SoftwareDTO();
                    PopularDto(reader, item);
                    return item;
                }
                else
                    return null;
            }
            catch (SqlException)
            {
                throw new Exception("Erro junto a comunicação com o banco");
            }
            catch (Exception)
            {
                throw new Exception("Erro inesperado");
            }
            finally
            {
                comando.Dispose();
            }
        }

        public void DeletarSoftware()
        { }

        #endregion

        #region Métodos Internos

        internal void PopularDto(IDataReader reader, SoftwareDTO item)
        {
            int var = reader.FieldCount;
            item.ID = (int)reader["ID"];
            item.Nome = (string)reader["Nome"];
            item.Versao = (string)reader["Versao"];
            item.Categoria = (string)reader["Categoria"];
            item.Fabricante = (string)reader["Fabricante"];
            item.Serial = (string)reader["Serial"];
        }

        internal IDbCommand CriarComandoBuscarSoftware()
        {
            return factory.CreateCommand("SELECT * FROM SOFTWARE");
        }

        internal IDbCommand CriarComandoBuscarSoftware(int etiqueta)
        {
 	        return factory.CreateCommand("SELECT * FROM SOFTWARE WHERE ID = '" + etiqueta + "'");
        }

        internal IDbCommand CriarComandoBuscarSoftware(string nomeColuna, string valorColuna)
        {
            return factory.CreateCommand("SELECT * FROM SOFTWARE WHERE " + nomeColuna + " = " + valorColuna + "");
        }

        #endregion
    }
}